package com.pet.web.controller.system;

import com.pet.common.core.controller.BaseController;
import com.pet.common.core.domain.AjaxResult;
import com.pet.common.core.page.TableDataInfo;
import com.pet.common.utils.poi.ExcelUtil;
import com.pet.system.domain.PetCart;
import com.pet.system.service.IPetCartService;
import com.pet.web.controller.system.vo.CartBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 *
 * @author pet
 * @date 2022-10-21
 */
//@Api(value = "采购单管理", tags = "采购单管理")
@RestController
@RequestMapping("/system/cart")
public class PetCartController extends BaseController {
    @Autowired
    private IPetCartService petCartService;

    /**
     * 查询购物车列表
     */
    //@ApiOperation("查询采购单列表")
    //@ApiImplicitParam(name = "cartSelectBody", value = "cartSelectBody", dataTypeClass = CartSelectBody.class)
    @PreAuthorize("@ss.hasPermi('system:cart:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PetCart petCart) {
        startPage();
        List<PetCart> list = petCartService.selectPetCartList(petCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('system:cart:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PetCart petCart) {
        List<PetCart> list = petCartService.selectPetCartList(petCart);
        ExcelUtil<PetCart> util = new ExcelUtil<PetCart>(PetCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(petCartService.selectPetCartById(id));
    }

    /**
     * 新增购物车
     */
    //@ApiOperation("添加到采购单")
    //@ApiImplicitParam(name = "cartBody", value = "cartBody", dataTypeClass = CartBody.class)
    @PreAuthorize("@ss.hasPermi('system:cart:add')")
    @PostMapping
    public AjaxResult add(@RequestBody CartBody cartBody) {
        PetCart petCart = new PetCart();
        petCart.setProductId(cartBody.getProductId());
        return toAjax(petCartService.insertPetCart(petCart));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('system:cart:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody PetCart petCart) {
        return toAjax(petCartService.updatePetCart(petCart));
    }

    /**
     * 删除购物车
     */
    //@ApiOperation("删除出采购单")
    @PreAuthorize("@ss.hasPermi('system:cart:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(petCartService.deletePetCartById(id));
    }
}
